/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vv;
        if (root == nullptr)
        {
            return vv;
        }

        queue<TreeNode*> root_q;
        queue<int> h_q;
        root_q.push(root);
        h_q.push(0);
        while (!root_q.empty())
        {
            TreeNode* node = root_q.front();
            root_q.pop();
            int index = h_q.front();
            h_q.pop();
            if (index >= vv.size())
            {
                vv.push_back(vector<int>());
            }
            vv[index].push_back(node->val);
            if (node->left)
            {
                root_q.push(node->left);
                h_q.push(index + 1);
            }
            if (node->right)
            {
                root_q.push(node->right);
                h_q.push(index + 1);
            }

        }
        return vv;
    }
};